package com.yunti.springboot.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunti.springboot.mapper.ScoreRuleMapper;
import com.yunti.springboot.mapper.SubItemMapper;
import com.yunti.springboot.mapper.SubjectMapper;
import com.yunti.springboot.po.ScoreRule;
import com.yunti.springboot.po.SubItem;
import com.yunti.springboot.po.Subject;
import com.yunti.springboot.service.ScoreRuleService;
import com.yunti.springboot.util.ScoreUtils;
import com.yunti.springboot.vo.Result;
import com.yunti.springboot.vo.req.ScoreRuleReq;
import com.yunti.springboot.vo.res.GetScoreRuleListRes;
import com.yunti.springboot.vo.res.ScoreRuleRes;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ScoreRuleServiceImpl extends ServiceImpl<ScoreRuleMapper, ScoreRule> implements ScoreRuleService {

    @Resource
    private SubItemMapper subItemMapper;


    @Override
    public Result getScoreRuleList(Integer subItemId) {
        GetScoreRuleListRes getScoreRuleListRes = new GetScoreRuleListRes();
        LambdaQueryWrapper<ScoreRule> scoreRuleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        scoreRuleLambdaQueryWrapper.eq(ObjectUtil.isNotNull(subItemId),ScoreRule::getSubItemId, subItemId);
        List<ScoreRule> scoreRuleList = baseMapper.selectList(scoreRuleLambdaQueryWrapper);

        Map<Integer, List<ScoreRule>> scoreRuleMap = scoreRuleList.stream().collect(Collectors.groupingBy(ScoreRule::getSexType));
        getScoreRuleListRes.setScoreRuleList(scoreRuleMap.get(0));
        getScoreRuleListRes.setBoyScoreRuleList(scoreRuleMap.get(1));
        getScoreRuleListRes.setGirlScoreRuleList(scoreRuleMap.get(2));
        return Result.success(getScoreRuleListRes);
    }


    @Override
    public Result getScoreByRawScore(Integer sex, Integer subItemId, String rawScore) {
        SubItem subItem = subItemMapper.selectById(subItemId);
        ScoreRule scoreRule = null;
        if (subItem == null) {
            return Result.error("查询失败");
        }
//        1.越大越好 2.越小越好
        if (Integer.valueOf(1).equals(subItem.getScoreType()) || Integer.valueOf(2).equals(subItem.getScoreType())) {

            LambdaQueryWrapper<ScoreRule> scoreRuleLambdaQueryWrapper = new LambdaQueryWrapper<>();
            scoreRuleLambdaQueryWrapper.eq(ScoreRule::getSubItemId, subItemId);
            if (NumberUtil.isNumber(rawScore)) {
                if (subItem.getScoreType() == 1) {
                    scoreRuleLambdaQueryWrapper.le(ScoreRule::getRawScore, Double.valueOf(rawScore));
                }
                if (subItem.getScoreType() == 2) {
                    scoreRuleLambdaQueryWrapper.ge(ScoreRule::getRawScore, Double.valueOf(rawScore));
                }
            }
            //时间格式
            else if(rawScore.contains(":")){
                if (subItem.getScoreType() == 1) {
                    scoreRuleLambdaQueryWrapper.le(ScoreRule::getRawScore, ScoreUtils.formatTimeStr(rawScore));
                }
                if (subItem.getScoreType() == 2) {
                    scoreRuleLambdaQueryWrapper.ge(ScoreRule::getRawScore,  ScoreUtils.formatTimeStr(rawScore));
                }
            }

            scoreRuleLambdaQueryWrapper.orderByDesc(ScoreRule::getConvScore);
            scoreRuleLambdaQueryWrapper.last("LIMIT 1");
            scoreRuleLambdaQueryWrapper.and(l -> l.eq(ScoreRule::getSexType, 0).or().eq(ScoreRule::getSexType, sex));
            scoreRule = baseMapper.selectOne(scoreRuleLambdaQueryWrapper);
        }
        //如果分数是等级制
        if (Integer.valueOf(3).equals(subItem.getScoreType())) {

        }
        //如果分数是扣分制
        if (Integer.valueOf(4).equals(subItem.getScoreType())) {

        }
        //如果分数是打分制
        if (Integer.valueOf(5).equals(subItem.getScoreType())) {

        }
        return Result.success(scoreRule);
    }

    @Override
    public Result findPage(ScoreRuleReq req) {
        if (req.getCurrent() == null) {
            req.setCurrent(1);
        }
        if (req.getSize() == null) {
            req.setSize(10);
        }
        Page<ScoreRuleRes> page = new Page<ScoreRuleRes>(req.getCurrent() , req.getSize());
        page.setRecords(baseMapper.findPage(page,req));
        return Result.success(page);
    }

    @Override
    public Result addScoreRole(ScoreRule rule) {
        if(baseMapper.insert(rule)>0){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @Override
    public Result updateScoreRoleById(ScoreRule rule) {
        if(baseMapper.updateById(rule)>0){
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @Override
    public Result delScoreRoleById(ScoreRule rule) {
        if(baseMapper.deleteById(rule)>0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
}
